package com.xii.eduVod.controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.xii.commonutils.R;
import com.xii.eduVod.service.VideoService;
import com.xii.eduVod.utils.AliyunVodSDKUtils;
import com.xii.eduVod.utils.ConstantPropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description = "阿里云视频点播服务")
//@CrossOrigin
@RestController
@RequestMapping("/vod/video")
public class VideoAdminController {
    @Autowired
    private VideoService videoService;
    @PostMapping("upload")
    @ApiOperation(value = "文件上传")
    public R uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {
        String videoId = videoService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId", videoId);
    }


    /**
     * @description: 删除视频
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @DeleteMapping("{videoId}")
    public R removeVideo(@PathVariable String videoId){
        videoService.removeVideo(videoId);
        return R.ok().message("视频删除成功");
    }



    //删除多个阿里云视频的方法
    //参数多个视频id  List videoIdList
    @DeleteMapping("deletebatch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        videoService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }

    /**
     * @description: 请求返回视频凭证
     *
     * @return {@link null}
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @GetMapping("getPlayAuth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {
        //获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);
        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        //得到播放凭证
        String playAuth = response.getPlayAuth();
        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}
